package com.jingdong.dao.order.impl;

import com.jingdong.dao.order.OrderDao;
import com.jingdong.model.Order_info;
import com.jingdong.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class OrderDaoImpl implements OrderDao {
    @Override
    public int save(Order_info order) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            int saveResult=sqlSession.insert("com.jingdong.orderModel.save",order);
            sqlSession.commit();
            sqlSession.close();
            return saveResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Order_info getById(int id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            Order_info order=sqlSession.selectOne("com.jingdong.orderModel.getById",id);
            sqlSession.close();
            return order;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Order_info order) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            if(order.getPay_status()==5){
                int updateResult=sqlSession.update("com.jingdong.orderModel.taskUpdate",order);
                sqlSession.commit();
                sqlSession.close();
                return updateResult;
            }else {
                int updateResult=sqlSession.update("com.jingdong.orderModel.update",order);
                sqlSession.commit();
                sqlSession.close();
                return updateResult;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Order_info> list(Map<String,Integer> map) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
            List<Order_info> orderList=sqlSession.selectList("com.jingdong.orderModel.list",map);
            sqlSession.close();
            return orderList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order_info> orderTaskList(int status) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
            List<Order_info> orderTaskList=sqlSession.selectList("com.jingdong.orderModel.taskList",status);
            sqlSession.close();
            return orderTaskList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
